package com.vonchange.headb.core.map;

import java.util.Map;

import com.vonchange.headb.core.constant.HeaConstant;
import com.vonchange.headb.utils.convert.HConvertUtils;
import com.vonchange.headb.utils.map.HHashMap;
import com.vonchange.headb.utils.map.HLinkedHashMap;

/**
 * 设置查询参数
 * @author fengchangyi
 * @date 2014-11-6 上午9:16:45
 */
public class HeaMap extends HLinkedHashMap<String, Object> implements
		Map<String, Object> {
	private static final long serialVersionUID = -6169144882263038794L;
	private String addSelect;//原columnSql xml中字段ID
	private int addOrder = 0;
	private int addGroup = 0;
	private Map<String,Object> viewParams= new HHashMap<String, Object>();
	private Map<String,Boolean> updateNullMap=new HHashMap<String, Boolean>();
	public HeaMap() {
	}

	/**
	 * 普通map转换到HeaMap
	 * @param map
	 */
	public HeaMap(Map<String, Object> map) {
		if (null == map) {
			map = new HeaMap();
		}
		for (Entry<String, Object> entry : map.entrySet()) {
			this.set(entry.getKey(), entry.getValue());
		}
		// super(map);
	}

	/**
	 * 普通map设置到HeaMap
	 * @param map
	 * @return
	 */
	public HeaMap setAll(Map<String, Object> map) {
		if (null == map) {
			map = new HeaMap();
		}
		for (Entry<String, Object> entry : map.entrySet()) {
			this.set(entry.getKey(), entry.getValue());
		}
		return this;
	}

	/**
	 * 设置参数
	 * @param key
	 * @param value
	 * @return HeaMap
	 */
	public HeaMap set(String key, Object value) {
		if (key.startsWith(HeaConstant.Instruction.ADDORDER)) {
			addOrder++;
			super.put(HeaConstant.Instruction.ADDORDER+ addOrder, value);
			return this;
		}
		if (key.startsWith(HeaConstant.Instruction.ADDGROUP)) {
			addGroup++;
			super.put(HeaConstant.Instruction.ADDGROUP+ addGroup, value);
			return this;
		}
		super.put(key, value);
		return this;
	}

	/**
	 * 设置数组
	 * @param key
	 * @param value
	 * @return HeaMap
	 */
	public HeaMap sets(String key, Object... value) {
		Object valueTemp = value;
		return this.set(key, valueTemp);
	}
	public HeaMap setSql(String sqlId, Object value) {
		return this.set(sqlId+"_sql", value);
	}
	public HeaMap addSql(String sqlId) {
		return this.set(sqlId+"_sql", null);
	}
	/**
	 * 不允许使用！
	 * @param key
	 * @param value
	 * @return null
	 */
	public HeaMap put(String key, Object value) {
		throw new RuntimeException("不允许使用put！！");
	}
	/**
	 * 设置排序参数
	 * @param orders
	 * @return HeaMap
	 */
	public HeaMap setAddOrder(String... orders) {
		for (String order : orders) {
			this.set(HeaConstant.Instruction.ADDORDER, order);
		}
		return this;
	}
	/**
	 * 设置分组参数
	 * @param groups
	 * @return HeaMap
	 */
	public HeaMap setAddGroup(String... groups) {
		for (String group : groups) {
			this.set(HeaConstant.Instruction.ADDGROUP, group);
		}
		return this;
	}
	/**
	 * 设置查询结果的开始条数
	 * @param value
	 * @return HeaMap
	 */
	public HeaMap setAddBeginNum(Integer beginNum) {
		this.set(HeaConstant.Instruction.ADDBEGINNUM, beginNum);
		return this;
	}

	/**
	 * 设置查询结果的结束条数
	 * @param value
	 * @return HeaMap
	 */
	public HeaMap setAddEndNum(Integer  endNum) {
		this.set(HeaConstant.Instruction.ADDENDNUM, endNum);
		return this;
	}
	/**
	 * 设置挑取的字段
	 * @param value
	 * @return HeaMap
	 */
	public HeaMap setAddSelect(String columnId) {
		this.set(HeaConstant.Instruction.ADDSELECT, columnId);
		return this;
	}


	public String getAddSelect() {
		if(null==addSelect){
			this.addSelect=HConvertUtils.toString(this.get(HeaConstant.Instruction.ADDSELECT));
		}
		return addSelect;
	}

	public Map<String, Object> getViewParams() {
		return viewParams;
	}

	/**
	 * 设置hea视图的参数
	 * @param key
	 * @param value
	 * @return HeaMap
	 */
	public HeaMap setViewParam(String key,Object value) {
		this.viewParams.put(key, value);
		return this;
	}



	public Map<String, Boolean> getUpdateNullMap() {
		return updateNullMap;
	}

	/**
	 * 设置更新为null的字段
	 * @param fieldName
	 * @return
	 */
	public HeaMap setUpdateNullFields(String... fieldName) {
		for (String str : fieldName) {
			this.updateNullMap.put(str, true);
		}		
		return this;
	}

}
